#include "rsumgr.h"

RsuMgr * RsuMgr::s_pInstance = NULL;

RsuMgr * RsuMgr::getInstance()
{
    if(!s_pInstance)
    {
        s_pInstance = new RsuMgr;
    }
    return s_pInstance;
}

RsuMgr::RsuMgr(QObject *parent):QObject(parent)
{
    m_RsuMap.clear();
}

RsuCtrl* RsuMgr::getRsuDev(int nIndex)
{
    QMap<int, RsuCtrl *>::const_iterator it = m_RsuMap.find(nIndex);
    if (it!=m_RsuMap.end())
    {
        RsuCtrl *pDev = it.value();
        if (NULL!=pDev)
        {
            return pDev;
        }
    }
    qDebug()<< "设备不存在";
    return NULL;
}

bool RsuMgr::setDevType(int nIndex)
{
    QMap<int, RsuCtrl *>::const_iterator it = m_RsuMap.find(nIndex);
    if (it!=m_RsuMap.end())
    {
        RsuCtrl *pDev = it.value();
        if (NULL!=pDev)
        {
            qDebug()<< "设备已存在";
            return true;
        }
    }
    RsuCtrl *pDev=new RsuCtrl(nIndex);

    if(NULL == pDev)
        return false;

    connect(pDev,&RsuCtrl::newFrameArrived,this,&RsuMgr::emitNewFramArrived);
    connect(pDev,&RsuCtrl::rsuInited,this,&RsuMgr::emitRsuInited);
    connect(pDev,&RsuCtrl::rsuInfoGot,this,&RsuMgr::emitRsuInfoGot);
    connect(pDev,&RsuCtrl::obuDetected,this,&RsuMgr::emitObuDetected);
    connect(pDev,&RsuCtrl::statusChanged,this,&RsuMgr::emitStatusChanged);

    m_RsuMap[nIndex]=pDev;
    return true;
}

void RsuMgr::releaseDev(int nIndex)
{
    RsuCtrl* pRsuDev = getRsuDev(nIndex);
    if(NULL != pRsuDev)
    {
        delete pRsuDev;
        pRsuDev = NULL;
    }
}

bool RsuMgr::openRsu(int nIndex,const QString &host, quint16 port)
{
    RsuCtrl* pRsuDev = getRsuDev(nIndex);
    if(NULL != pRsuDev)
    {
       pRsuDev->openRsu(host,port);
       return true;
    }
    return false;
}

bool RsuMgr::reopenRsu(int nIndex)
{
    RsuCtrl* pRsuDev = getRsuDev(nIndex);
    if(NULL != pRsuDev)
    {
        pRsuDev->tryReconnect();
        return true;
    }
    return false;
}
bool RsuMgr::closeRsu(int nIndex)
{
    RsuCtrl* pRsuDev = getRsuDev(nIndex);
    if(NULL != pRsuDev)
    {
        pRsuDev->closeRsu();
        return true;
    }
    return false;
}

int RsuMgr::initRsu(int nIndex,int nBSTInterval, int nTxPower, int nPLLChannelID)
{
    RsuCtrl* pRsuDev = getRsuDev(nIndex);
    if(NULL != pRsuDev)
    {
        pRsuDev->initRsu(nBSTInterval,nTxPower,nPLLChannelID);
        return true;
    }
    return false;
}

int RsuMgr::getRsuInfo(int nIndex)
{
    RsuCtrl* pRsuDev = getRsuDev(nIndex);
    if(NULL != pRsuDev)
    {
        pRsuDev->getRsuInfo();
        return true;
    }
    return false;
}

int RsuMgr::detectObu(int nIndex)
{
    RsuCtrl* pRsuDev = getRsuDev(nIndex);
    if(NULL != pRsuDev)
    {
        pRsuDev->detectObu();
        return true;
    }
    return false;
}

bool RsuMgr::checkRsuStatus(int nIndex)
{
    RsuCtrl* pRsuDev = getRsuDev(nIndex);
    if(NULL != pRsuDev)
    {
       pRsuDev->checkRsuStatus();
       return true;
    }
    return false;
}

bool RsuMgr::tryReconnect(int nIndex)
{
    RsuCtrl* pRsuDev = getRsuDev(nIndex);
    if(NULL != pRsuDev)
    {
        pRsuDev->tryReconnect();
        return true;
    }
    return false;
}

void RsuMgr::setConfigHeartBeat(int nIndex,int nStyle,int nInterval)
{
    RsuCtrl* pRsuDev = getRsuDev(nIndex);
    if(NULL != pRsuDev)
        pRsuDev->setHeartBeat(nStyle,nInterval);
}

bool RsuMgr::startDetectObu(int nIndex,int nInterval)
{
    RsuCtrl* pRsuDev = getRsuDev(nIndex);
    if(NULL != pRsuDev)
    {
        pRsuDev->startDetectObu(nInterval);
        return true;
    }
    return false;
}

bool RsuMgr::stopDetectObu(int nIndex)
{
    RsuCtrl* pRsuDev = getRsuDev(nIndex);
    if(NULL != pRsuDev)
    {
        pRsuDev->stopDetectObu();
        return true;
    }
    return false;
}

void RsuMgr::releaseObu(int nIndex)
{
    RsuCtrl* pRsuDev = getRsuDev(nIndex);
    if(NULL != pRsuDev)
        pRsuDev->releaseObu();
}

void RsuMgr::setBeaconChangeInterval(int nIndex,int nInterval)
{
    RsuCtrl* pRsuDev = getRsuDev(nIndex);
    if(NULL != pRsuDev)
        pRsuDev->setBeaconChangeInterval(nInterval);
}

void RsuMgr::emitNewFramArrived(quint8 nFrameIndex, quint8 nFrameType,const QByteArray &frame)
{
    RsuCtrl* sc = dynamic_cast<RsuCtrl*>(sender());
    if(NULL != sc)
    {
        int index =sc->getId();
        emit newFrameArrived(index,nFrameIndex,nFrameType,frame);
    }
}
void RsuMgr::emitRsuInited(int nRsuStatus,const QByteArray &baRsuInfo)
{
    RsuCtrl* sc = dynamic_cast<RsuCtrl*>(sender());
    if(NULL != sc)
    {
        int index =sc->getId();
        emit rsuInited(index,nRsuStatus,baRsuInfo);
    }
}
void RsuMgr::emitRsuInfoGot(const QByteArray &baRsuInfo)
{
    RsuCtrl* sc = dynamic_cast<RsuCtrl*>(sender());
    if(NULL != sc)
    {
        int index =sc->getId();
        emit rsuInfoGot(index,baRsuInfo);
    }
}
//void RsuMgr::emitObuDetected(ObuInfo newObu, IccInfo newIcc)
void RsuMgr::emitObuDetected(ObuInfo newObu)
{
    RsuCtrl* sc = dynamic_cast<RsuCtrl*>(sender());
    if(NULL != sc)
    {
        int index =sc->getId();
        emit obuDetected(index,newObu);
    }
}
void RsuMgr::emitStatusChanged(int status)
{
    RsuCtrl* sc = dynamic_cast<RsuCtrl*>(sender());
    if(NULL != sc)
    {
        int index =sc->getId();
        emit statusChanged(index,status);
    }
}
